package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserListenProcessApiController {

    @Autowired
    private UserListenProcessService userListenProcessService;

    /**
     * 查询当前用户指定的声音播放进度
     *
     * @param trackId
     * @return
     */
    @GuiguLogin(required = false)
    @Operation(summary = "查询当前用户指定的声音播放进度")
    @GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
    public Result<BigDecimal> getTrackBreakSecond(@PathVariable Long trackId) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            BigDecimal breakSecond = userListenProcessService.getTrackBreakSecond(userId, trackId);
            return Result.ok(breakSecond);
        }
        //TODO 如果未登录，则将响应时间设置为0.00
        return Result.ok(new BigDecimal(0.00));
    }

    /**
     * 保存或者更新当前用户指定的声音播放进度
     *
     * @param userListenProcessVo
     * @return
     */
    @GuiguLogin(required = false)
    @Operation(summary = "保存或者更新当前用户指定的声音播放进度")
    @PostMapping("/userListenProcess/updateListenProcess")
    public Result updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            userListenProcessService.updateListenProcess(userId, userListenProcessVo);
        }
        return Result.ok();
    }

    /**
     * 获取当前用户上次播放专辑以及声音达到快速播放的效果
     *
     * @return
     */
    @Operation(summary = "获取当前用户上次播放专辑以及声音达到快速播放的效果")
    @GuiguLogin(required = false)
    @GetMapping("/userListenProcess/getLatelyTrack")
    public Result<UserListenProcessVo> getLatelyTrack() {
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            UserListenProcessVo userListenProcessVo = userListenProcessService.getLatelyTrack(userId);
            return Result.ok(userListenProcessVo);
        }
        return Result.ok();
    }

}

